package cn.dy.sbo.web.sys;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import cn.common.lib.springside.web.CrudActionSupport;
import cn.dy.sbo.core.UserManager;
import cn.dy.sbo.model.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Namespace("/sys")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "user.action", type = "redirect") })
public class UserAction extends ActionSupport implements Preparable
{
    private static final long serialVersionUID = 8683878162525847072L;

    // -- 页面属性 --//

    private Long              id;                                     // 全局Id

    private Page<User>        page             = new Page<User>(10);  // 每页10条记录

    private User              user;                                   // 定义用户

    @Autowired
    private UserManager       userManager;

    /**
     * 获得某个项目下的用户列表,可按用户名和权限等级查询
     * 
     * @since 2011-11-8
     * @see cn.common.lib.springside.web.CrudActionSupport#list()
     */
    @Override
    public String execute() throws Exception
    {
        List<PropertyFilter> filters = PropertyFilter
                .buildFromHttpRequest(Struts2Utils.getRequest());
        page = userManager.search(page, filters);
        return SUCCESS;
    }

    /**
     * 删除用户
     * 
     */
    public String delete() throws Exception
    {
        return "reload";
    }

    /**
     * 进入保存用户页面
     * 
     * @since 2011-11-10
     * @see cn.common.lib.springside.web.CrudActionSupport#input()
     */
    @Override
    public String input() throws Exception
    {
        return INPUT;
    }

    /**
     * 保存用户信息
     * 
     * @since 2011-11-8
     * @see cn.common.lib.springside.web.CrudActionSupport#save()
     */
    public String save()
    {
        userManager.save(user);
        return "reload";
    }

    public String used()
    {
        try
        {
            userManager.used(id);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "reload";
    }

    public String unUsed()
    {
        try
        {
            userManager.unUsed(id);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "reload";
    }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Page<User> getPage()
    {
        return page;
    }

    public void setPage(Page<User> page)
    {
        this.page = page;
    }

    public void prepareSave() throws Exception
    {
        prepare();
    }

    public void prepareInput() throws Exception
    {
        prepare();
    }

    @Override
    public void prepare() throws Exception
    {
        if (null != id)
        {
            user = userManager.get(id);
        }
        else
        {
            user = new User();
        }
    }
}